package com.cooljie;

/**
 * @ClassNamehdfs
 * @Description
 * @Author
 * @Date2022/3/30 10:42
 **/

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/***
 * 1.获取客户端对象
 * 2.执行相关操作
 * 3.关闭资源
 */

public class HdfsClient {
    /* https://github.com/cdarlint/winutils.git
  https://blog.csdn.net/m0_45259293/article/details/115278494
   }*/
   private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // 集群nn地址
        URI uri = new URI("hdfs://node01:8020");
        fs = FileSystem.get(uri, new Configuration(),"root");

    }

    @After
    public void close() throws IOException {
        fs.close();
    }


    /**
     * 创建目录
     * @throws IOException
     */
    @Test
    public  void testMkdir() throws IOException {
        boolean mkdirs = fs.mkdirs(new Path("/sanguo/zj"));
        System.out.println(mkdirs);
    }

    /**
     * 上传文件
     * @throws IOException
     */
    @Test
    public void testUpload() throws IOException {
        fs.copyFromLocalFile(false,new Path("E://learn//wuguo.txt"),new Path("/sanguo/wuguo.txt"));
    }



    @Test
    public void testDownLoad() throws IOException {
        fs.copyToLocalFile(false,new Path("/sanguo/wuguo.txt"),new Path("E://learn/wuguo1.txt"),false);
    }


    /***
     * 删除
     * @throws IOException
     */
    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/sanguo/wuguo.txt"),false);
    }

    /**
     * 文件移动与更名
     * @throws IOException
     */
    @Test
    public void testMove() throws IOException {
        fs.rename(new Path("/sanguo/wuguo.txt"),new Path("/sanguo/wuguo1.txt"));
    }


    /**
     * 查询文件详情
     */
    @Test
    public void testDetail() throws IOException {
        RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/sanguo"), true);
        while (locatedFileStatusRemoteIterator.hasNext()) {
            LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next();
            System.out.println("====================");
            System.out.printf(fileStatus.getPermission().toString()+"---");
            System.out.printf(fileStatus.getOwner() +"---");
            System.out.printf(fileStatus.getGroup() +"---");
            System.out.printf(fileStatus.getLen()+"---");
            System.out.printf(fileStatus.getModificationTime()+"---");
            System.out.printf(fileStatus.getReplication()+"---");
            System.out.printf(fileStatus.getBlockSize()+"---");
            System.out.printf(fileStatus.getPath().getName()+"---");
        }
    }


    /**
     * 文件及文件夹判断
     * @throws IOException
     */
    @Test
    public void testFileOrDir() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/sanguo"));

        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isDirectory()){
                System.out.println("文件===================="+fileStatus.getPath().getName());
            }else {
                System.out.println("目录===================="+fileStatus.getPath().getName());
            }
        }
    }
}
